<?php
	
	class Controller_Category extends Controller
	{
        private $lang = 1;
        
        function __construct($registry)
        {
            parent::__construct($registry);
            $this->_registry['smarty']->assign('id_page', 'menu_manage');  
        } 
        
        /**
        * manage News Category
        * 
        * @param array $args
        * @author ngviettoan79@gmail.com
        * @since 22/12/2008
        */
        public function index($args)
        {
        	$smarty =  $this->_registry['smarty'];
            $objCategoryModel = new Model_Category_Access();   
            $arrObjCategory = $objCategoryModel->showAll($this->lang);
            $smarty->assign('categories', $arrObjCategory);
           	$smarty->display('category.tpl');
        }
        
        /**
        * edit existing Category
        * 
        * @param array $args
        * @author ngviettoan79@gmail.com
        * @since 22/12/2008
        */
        public function edit($args)
        {
        	$smarty =  $this->_registry['smarty'];
        	$strError	=	'';
        	$strSuccess =	'';
        	$category	=	array();
        	$category['id'] = isset($args[0]) ? $args[0] : 0;

        	//check valid $category['id']
        	if($category['id'] > 0){
        		//check existing category in database
        		$objCategoryModel = new Model_Category_Access();
        		$categories = $objCategoryModel->checkExistCategory($category['id'], $this->lang) ;
        		//if existings
        		
        		if(!is_null($categories)) {
					$category['name'] = $categories->cl_name;
					$category['description'] = $categories->cl_description;
					if(isset($_POST['submit'])) {
					 	$category['name'] = isset($_POST['name']) ? $_POST['name'] : '';
    					$category['description'] = isset($_POST['description']) ? $_POST['description'] : '';
    					
    					if(empty($category['name'])) {
    						$strError[] = "Tên danh mục là bắt buộc";
						}
    					if(empty($category['description'])) {
    						$strError[] = "Thông tin mô tả là bắt buộc";
						}
						else {
							//update category_language
							$objCategoryModel->updateCategoryLanguage($category['name'], $category['description'], $category['id'], $this->lang);
							$strSuccess[] = 'Cập nhật danh mục Thành Công';	
						}
					}
				}
			}
        	
        	$smarty->assign(array(	'strError' => $strError,
							  		'strSuccess' => $strSuccess,
        					  		'category' => $category));
        	$smarty->display('category_edit.tpl');        
		}
		
		/**
        * delete specific Category
        * 
        * @param array $id (real $id = $id[0])
        * @author ngviettoan79@gmail.com
        * @since 22/12/2008
        */
		public function del($id)
		{
			$smarty = $this->_registry['smarty'];
			$cat_id = $id[0];
			$objCategoryModel = new Model_Category_Access();
			$result = $objCategoryModel->delete($cat_id);
			header('Location: '.BASEURL.'/category');
		}
		
		/**
		* Add new Category
		* 
        * @author ngviettoan79@gmail.com
        * @since 22/12/2008
		*/
		public function add()
		{
			$smarty = $this->_registry['smarty'];
			
			$strError	=	'';
        	$strSuccess =	'';
        	$category	=	array();
        	$lang = $GLOBALS['lang'];
        	
			if(isset($_POST['submit'])) {
				$category['name'] = isset($_POST['name'])? $_POST['name']:'';    
	        	$category['description'] = isset($_POST['description'])? $_POST['description']:'';  
	        	
	        	if(strlen($category['name']) == 0) {
	        		$strError[] = "Tên danh mục là bắt buộc";
				}
				
				if(strlen($category['description']) == 0) {
			    	$strError[] = "Thông tin mô tả là bắt buộc"; 
				}
				
				else {
					//get max order
					$objCategoryModel = new Model_Category_Access();
					$maxOrder = $objCategoryModel->getMaxOrder();
					//insert category
					$objCategoryModel->insertCategory($maxOrder+1);
					// get lasted c_id
					$c_id =  $objCategoryModel->getLastestCatId();
					//insert category_language
					$objCategoryModel->insertCategoryLanguage($c_id, $this->lang, $category['name'], $category['description']);
					$strSuccess[] = 'Thêm Danh mục mới thành công';
					$category = '';
				} 	
			}	
			$smarty->assign(array('strError' => $strError,
							  'strSuccess' => $strSuccess,
        					  'category' => $category));
			//display 
			$smarty->display('category_add.tpl');              
		}
	}
?>